/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package jas.http.response;

/**
 *
 * @author Asger
 */
public class HttpResponseFormatter {
    
    public static HttpResponse parseResponse(String str) {
        
        HttpResponse response = new HttpResponse();
        
        int splitAt = str.indexOf("\r\n\r\n");
        
        if (splitAt == -1)
            splitAt = str.length();
        
        String header = str.substring(0, splitAt);
        String body = "";
        if (splitAt < str.length()) {
            body = str.substring(splitAt + "\r\n\r\n".length());
        }
        
        String[] headerLines = header.split("\r\n");
        
        String responseLine = headerLines[0];
        String[] responseLineParts = responseLine.split(" ");
        
        int code = Integer.valueOf(responseLineParts[1]);
        response.setCode(ResponseCode.fromNumber(code));
        
        response.setBody(new StringResponseBody(body));
        
        return response;
    }
    
}
